package com.xsd.xsdhouduan.controller;

import com.xsd.xsdhouduan.common.Result;
import com.xsd.xsdhouduan.dto.AdminLoginRequest;
import com.xsd.xsdhouduan.dto.LoginRequest;
import com.xsd.xsdhouduan.dto.RegisterRequest;
import com.xsd.xsdhouduan.entity.Admin;
import com.xsd.xsdhouduan.entity.School;
import com.xsd.xsdhouduan.entity.User;
import com.xsd.xsdhouduan.service.AdminService;
import com.xsd.xsdhouduan.service.SchoolService;
import com.xsd.xsdhouduan.service.UserService;
import com.xsd.xsdhouduan.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 认证控制器
 */
@RestController
@RequestMapping("/api/auth")
@CrossOrigin
public class AuthController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private AdminService adminService;
    
    @Autowired
    private SchoolService schoolService;
    
    @Autowired
    private JwtUtil jwtUtil;
    
    /**
     * 用户登录
     */
    @PostMapping("/user/login")
    public Result<Map<String, Object>> userLogin(@Valid @RequestBody LoginRequest request) {
        User user = userService.login(request.getStudentId(), request.getPassword());
        if (user != null) {
            if (user.getStatus() == 0) {
                return Result.error("账户已被限制，请联系管理员");
            }
            
            String token = jwtUtil.generateToken(user.getStudentId(), "user");
            Map<String, Object> data = new HashMap<>();
            data.put("token", token);
            data.put("user", user);
            return Result.success("登录成功", data);
        }
        return Result.error("学号或密码错误");
    }
    
    /**
     * 管理员登录
     */
    @PostMapping("/admin/login")
    public Result<Map<String, Object>> adminLogin(@Valid @RequestBody AdminLoginRequest request) {
        Admin admin = adminService.login(request.getAccount(), request.getPassword());
        if (admin != null) {
            if (admin.getStatus() == 0) {
                return Result.error("账户已被限制");
            }
            
            String token = jwtUtil.generateToken(admin.getAccount(), "admin");
            Map<String, Object> data = new HashMap<>();
            data.put("token", token);
            data.put("admin", admin);
            return Result.success("登录成功", data);
        }
        return Result.error("账号或密码错误");
    }
    
    /**
     * 用户注册
     */
    @PostMapping("/user/register")
    public Result<String> userRegister(@Valid @RequestBody RegisterRequest request) {
        // 检查学号是否已存在
        if (userService.getByStudentId(request.getStudentId()) != null) {
            return Result.error("学号已存在");
        }
        
        // 查找学校
        School school = schoolService.getByName(request.getSchoolName());
        if (school == null) {
            return Result.error("学校不存在");
        }
        
        // 创建用户
        User user = new User();
        user.setStudentId(request.getStudentId());
        user.setName(request.getName());
        user.setPassword(request.getPassword());
        user.setSex(request.getSex());
        user.setEmail(request.getEmail());
        user.setSchoolId(school.getId());
        
        if (userService.register(user)) {
            return Result.success("注册成功");
        }
        return Result.error("注册失败");
    }
    
    /**
     * 获取学校列表
     */
    @GetMapping("/schools")
    public Result<Object> getSchools() {
        return Result.success(schoolService.list());
    }
}
